package jiyun.com.yiyanmeng;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import adapter.CommentDetailAdapter;
import base.BaseObserver;
import bean.SchoolPostCommentListResultBean;
import io.reactivex.Observable;
import jiyun.com.yiyanmeng.http.HttpManager;
import jiyun.com.yiyanmeng.http.MySerivce;
import utils.RxUtil;
import utils.ToastUtil;

//评论详情
public class SchoolPostCommentListActivity extends AppCompatActivity implements View.OnClickListener {

    /**
     * 返回
     */
    private TextView mImageBack;
    /**  */
    private TextView mTvTitle;
    private RecyclerView mRv;
    private int id;
    private List <SchoolPostCommentListResultBean.SchoolPostCommentBean> list = new ArrayList <>();
    private CommentDetailAdapter ada;
    private int page = 1;
    private SmartRefreshLayout mSmart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_school_post_comment_list);
//        EventBus.getDefault().register(this);  //事件的注册
        initView();
        mTvTitle.setText("评论详情");
        id = getIntent().getIntExtra("id", 0);
        Log.i("positioned", "onCreate: " + id);
        getData();
        initData();
    }

    private void getData() {
        MySerivce serivce = HttpManager.getInstance().getSerivce(MySerivce.base_URL, MySerivce.class);
        Observable <SchoolPostCommentListResultBean> pinbean2 = serivce.pinbean2(String.valueOf(id), page + "");
        pinbean2.compose(RxUtil. <SchoolPostCommentListResultBean>changeThread())
                .subscribe(new BaseObserver <SchoolPostCommentListResultBean>() {
                    @Override
                    public void onSuccess(SchoolPostCommentListResultBean bean) {
                        Log.i("bean", "onSuccess: " + bean.toString());
                        Log.i("bean", "onSuccess: " + bean.info);
                        list.addAll(bean.info);
                        ada.notifyDataSetChanged();
                    }

                    @Override
                    public void onFail(String error) {
                        Log.i("bean", "onFail: " + error);
                        ToastUtil.show(SchoolPostCommentListActivity.this, error);
                    }
                });
    }

    private void initData() {
        mRv.setLayoutManager(new LinearLayoutManager(this));
        ada = new CommentDetailAdapter(this, list);
        ada.setType(3);
        mRv.setAdapter(ada);
        mSmart.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getData();
                ada.notifyDataSetChanged();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                list.clear();
                page=1;
                ada.notifyDataSetChanged();
            }
        });
    }

    private void initView() {
        mImageBack = (TextView) findViewById(R.id.image_back);
        mImageBack.setOnClickListener(this);
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mRv = (RecyclerView) findViewById(R.id.rv);
        mSmart = (SmartRefreshLayout) findViewById(R.id.smart);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.image_back:
                finish();
                break;
        }
    }

}
